package com.ewit.colourlifepm.ui.contacts;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;

public class ContactInfo extends android.content.ContextWrapper {

	private ContentResolver cr = getContentResolver();

	public ContactInfo(Context base) {
		super(base);
	}

	public List<ContactsEntity> GetContactList(Boolean isWithGroupInfo) {
		List<ContactsEntity> list_ShowData = new ArrayList<ContactsEntity>();

		ContactsEntity showContacts = new ContactsEntity();

		Cursor cursor = cr
				.query(ContactsContract.Contacts.CONTENT_URI,
						new String[]{ContactsContract.Contacts._ID,
								ContactsContract.Contacts.DISPLAY_NAME,
								ContactsContract.Contacts.HAS_PHONE_NUMBER,
								"sort_key"}, null, null,
						"sort_key COLLATE LOCALIZED asc");

		String contactId = "";
		String name = "";
		String sort_key = "";
		String phoneNumber = "";
		String has_phoneNumber = "0";

		String company = "";
		while (cursor.moveToNext()) {

			has_phoneNumber = cursor
					.getString(cursor
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

			if (has_phoneNumber.equals("1")) {

				name = cursor
						.getString(cursor
								.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

				if (name != null) {

					contactId = cursor.getString(cursor
							.getColumnIndex(ContactsContract.Contacts._ID));

					sort_key = cursor.getString(cursor
							.getColumnIndex("sort_key"));
					has_phoneNumber = cursor
							.getString(cursor
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					showContacts = new ContactsEntity(name, company, phoneNumber,
							contactId, false, sort_key);
					list_ShowData.add(showContacts);
				}
			}
		}

		cursor.close();

		Pad_FirstPhone(list_ShowData);
		Pad_Company(list_ShowData);

		if (isWithGroupInfo) {
			Pad_Group(list_ShowData);
		}

		return list_ShowData;
	}

	private void Pad_FirstPhone(List<ContactsEntity> list_ShowData) {

		Cursor phones = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{ContactsContract.Data.CONTACT_ID,
								ContactsContract.CommonDataKinds.Phone.NUMBER},
						Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
								+ "'", null, null);

		String contactId = "";
		String phoneNumber = "";

		while (phones.moveToNext()) {

			contactId = phones.getString(phones
					.getColumnIndex(ContactsContract.Data.CONTACT_ID));

			phoneNumber = phones
					.getString(phones
							.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

			for (ContactsEntity showContacts : list_ShowData) {

				if (showContacts.getPhoneNumber().length() == 0) {

					if (showContacts.getContactId().equals(contactId)) {

						phoneNumber = phones
								.getString(phones
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						phoneNumber = phoneNumber == null ? "" : phoneNumber;
						phoneNumber = phoneNumber.replace("-", "");
						phoneNumber = phoneNumber.replace(" ", "");
						showContacts.setPhoneNumber(phoneNumber);
						break;
					}
				}
			}
		}

		phones.close();
	}

	private void Pad_Company(List<ContactsEntity> list_ShowData) {

		Cursor organizations = cr
				.query(ContactsContract.Data.CONTENT_URI,

						new String[]{
								ContactsContract.Data.CONTACT_ID,
								ContactsContract.CommonDataKinds.Organization.COMPANY,
								"sort_key"},

						Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
								+ "'", null, null);

		String contactId = "";
		String company = "";

		while (organizations.moveToNext()) {

			contactId = organizations.getString(organizations
					.getColumnIndex(ContactsContract.Data.CONTACT_ID));

			for (ContactsEntity showContacts : list_ShowData) {

				if (showContacts.getTextCompany().length() == 0) {

					if (showContacts.getContactId().equals(contactId)) {

						company = organizations
								.getString(organizations
										.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));

						company = company == null ? "" : company;
						showContacts.setTextCompany(company);
						break;
					}
				}
			}

		}

		organizations.close();
	}

	private void Pad_Group(List<ContactsEntity> list_ShowData) {

		ContactGroup contactGroup = new ContactGroup(this);
		HashMap<String, String> groupReference = contactGroup
				.GetGroupReference();

		Cursor GroupMembershipCursor = cr
				.query(ContactsContract.Data.CONTENT_URI,

						new String[]{
								ContactsContract.Data.CONTACT_ID,
								ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID},

						Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
								+ "'", null, "contact_id COLLATE LOCALIZED asc");

		String contactId = "";
		String group_ROW_ID = "";
		String groupTitle_Temp;
		String groupTitle_Temp2;

		while (GroupMembershipCursor.moveToNext()) {

			contactId = GroupMembershipCursor.getString(GroupMembershipCursor
					.getColumnIndex(ContactsContract.Data.CONTACT_ID));

			for (ContactsEntity showContacts : list_ShowData) {

				if (showContacts.getContactId().equals(contactId)) {

					group_ROW_ID = GroupMembershipCursor
							.getString(GroupMembershipCursor
									.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID));

					groupTitle_Temp = groupReference.get(group_ROW_ID);
					groupTitle_Temp2 = groupTitle_Temp == null
							? ""
							: groupTitle_Temp;

					if (groupTitle_Temp2.length() > 0) {

						if (showContacts.getGroupName() == null) {
							showContacts.setGroupName(groupTitle_Temp2 + ",");
						} else {
							showContacts.setGroupName(showContacts.getGroupName()
									+ groupTitle_Temp2 + ",");
						}
					} else {
						if (showContacts.getGroupName() == null) {
							showContacts.setGroupName("");
						}
					}

					break;
				}
			}
		}
		GroupMembershipCursor.close();

		for (ContactsEntity showContacts : list_ShowData) {
			if (showContacts.getGroupName() == null) {
				showContacts.setGroupName("");
			}
		}

	}

	private String getContactId_Combine(List<String> contactIds) {
		String contactId_Combine = "";
		for (String contactId : contactIds) {
			contactId_Combine += "'" + contactId + "',";
		}

		if (contactId_Combine.length() > 0) {
			contactId_Combine = contactId_Combine.substring(0,
					contactId_Combine.length() - 1);
		}

		return contactId_Combine;
	}

	private Collection<Contact> Generate_Contact_Submit_Form(
			List<String> contactIds) {
		Collection<Contact> contactHeader = new ArrayList<Contact>();

		DeviceId deviceId = new DeviceId(this);
		for (String contactId : contactIds) {

			Contact contact = new Contact();

			contact.setPhoneContactId(contactId);
			contact.setDeviceId(deviceId.getDeviceId());
			contact.setIdType(deviceId.getIdType());

			Collection<ContactItem> contactItemHeader = new ArrayList<ContactItem>();
			contact.setContactItem(contactItemHeader);

			contactHeader.add(contact);

		}

		return contactHeader;

	}

	private void Pad_names(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor names = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
								ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
								ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
								ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
								ContactsContract.CommonDataKinds.StructuredName.PREFIX,
								ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
								ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID},
						ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE + " =  ?",
						new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE},
						null);

		String contactId = "";
		String first_judge;
		String middle_judge;
		String last_judge;
		String prefix_judge;
		String suffix_judge;

		while (names.moveToNext()) {

			contactId = names
					.getString(names
							.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));

			for (Contact contact : contactHeader) {
				if (contact.getPhoneContactId().equals(contactId)) {
					first_judge = names
							.getString(names
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
					if (first_judge != null) {
						contact.setFirst(first_judge);
						// Log.i("GIVEN_NAME", first_judge);
					}

					middle_judge = names
							.getString(names
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
					if (middle_judge != null) {
						contact.setMiddle(middle_judge);
						// Log.i("MIDDLE_NAME", middle_judge);
					}

					last_judge = names
							.getString(names
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
					if (last_judge != null) {
						contact.setLast(last_judge);
						// Log.i("FAMILY_NAME", last_judge);
					}

					/*
					 * String initial_judge =
					 * names.getString(names.getColumnIndex(ContactsContract
					 * .CommonDataKinds.StructuredName.DISPLAY_NAME)); if
					 * (initial_judge != null) {
					 * contact.setInitial(initial_judge); //
					 * Log.i("DISPLAY_NAME", initial_judge); }
					 */

					prefix_judge = names
							.getString(names
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
					if (prefix_judge != null) {
						contact.setPrefix(prefix_judge);
						// Log.i("PREFIX", prefix_judge);
					}

					suffix_judge = names
							.getString(names
									.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
					if (suffix_judge != null) {
						contact.setSuffix(suffix_judge);
						// Log.i("SUFFIX", suffix_judge);
					}
					break;
				}
			}

		}
		names.close();

	}

	private void Pad_Event(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor EventCursor = cr
				.query(Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.Event.DATA,
								ContactsContract.CommonDataKinds.Event.TYPE,
								ContactsContract.CommonDataKinds.Event.CONTACT_ID},
						ContactsContract.CommonDataKinds.Event.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
								+ "'", null, null);

		if (EventCursor.moveToFirst()) {
			String contactId = "";
			String event;
			String eventType;

			do {

				contactId = EventCursor
						.getString(EventCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Event.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						event = EventCursor
								.getString(EventCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Event.DATA));
						eventType = EventType
								.getActualType(Integer.parseInt(EventCursor.getString(EventCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))));
						if (eventType == EventType.BIRTHDAY) {
							contact.setBirthday(event);
						}

						// Log.i("ContentProvider", "event:"+event);
						// Log.i("ContentProvider", "eventType:"+eventType);

						break;
					}
				}

			} while (EventCursor.moveToNext());
		}

		EventCursor.close();

	}

	private void Pad_Phones(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor phonesCursor = cr.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,
						ContactsContract.CommonDataKinds.Phone.TYPE,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID},
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " in("
						+ contactId_Combine + ")", null, null);

		if (phonesCursor.moveToFirst()) {

			String contactId = "";
			String phone;
			String phoneType;
			do {

				contactId = phonesCursor
						.getString(phonesCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						phone = phonesCursor
								.getString(phonesCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						phoneType = PhoneType
								.getActualType(Integer.parseInt(phonesCursor.getString(phonesCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))));

						// Log.i("ContentProvider", "PHONE: "+phone);
						// Log.i("ContentProvider", "PHONETYPE: "+phoneType);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Phone");
						contactItem.setSubtype(phoneType);
						contactItem.setValue(phone);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");

						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (phonesCursor.moveToNext());
		}
		phonesCursor.close();

	}

	private void Pad_Organization(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor orgCursor = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.Organization.COMPANY,
								ContactsContract.CommonDataKinds.Organization.TYPE,
								ContactsContract.CommonDataKinds.Organization.TITLE,
								ContactsContract.CommonDataKinds.Organization.CONTACT_ID},
						ContactsContract.CommonDataKinds.Organization.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE
								+ "'", null, null);

		if (orgCursor.moveToFirst()) {

			String contactId = "";
			String company;
			String title;
			String companyType;
			Object orgType;
			do {

				contactId = orgCursor
						.getString(orgCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Organization.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						company = orgCursor
								.getString(orgCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));

						orgType = orgCursor
								.getString(orgCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE));

						title = orgCursor
								.getString(orgCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));

						// Log.i("ContentProvider",
						// "Company:"+company+"/nTitle: "+title);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Company");
						if (orgType != null) {
							companyType = CompanyType
									.getActualType(Integer.parseInt(orgCursor.getString(orgCursor
											.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE))));
							contactItem.setSubtype(companyType);
						}
						contactItem.setValue(company);
						contactItem.setValue2(title);
						// contactItem.setCountry("");
						// contactItem.setRegion("");

						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (orgCursor.moveToNext());
		}
		orgCursor.close();

	}

	private void Pad_email(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor emailCursor = cr.query(
				ContactsContract.CommonDataKinds.Email.CONTENT_URI,
				new String[]{ContactsContract.CommonDataKinds.Email.DATA1,
						ContactsContract.CommonDataKinds.Email.TYPE,
						ContactsContract.CommonDataKinds.Email.CONTACT_ID},

				ContactsContract.CommonDataKinds.Email.CONTACT_ID + " in("
						+ contactId_Combine + ")", null, null);

		if (emailCursor.moveToFirst()) {

			String contactId = "";
			String email;
			String emailType;
			do {

				contactId = emailCursor
						.getString(emailCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						email = emailCursor
								.getString(emailCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1));
						emailType = EmailType
								.getActualType(Integer.parseInt(emailCursor.getString(emailCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))));

						// Log.i("ContentProvider", "Email: "+email);
						// Log.i("ContentProvider",
						// "Email:"+email+"/nEmailType: "+emailType);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Email");
						contactItem.setSubtype(emailType);
						contactItem.setValue(email);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");

						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (emailCursor.moveToNext());
		}
		emailCursor.close();

	}

	private void Pad_postal(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor postalCursor = cr
				.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
						new String[]{
								// ContactsContract.CommonDataKinds.StructuredPostal.STREET,
								// ContactsContract.CommonDataKinds.StructuredPostal.POBOX,
								// ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD,
								// ContactsContract.CommonDataKinds.StructuredPostal.CITY,
								ContactsContract.CommonDataKinds.StructuredPostal.REGION,
								// ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
								ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
								ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
								ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
								ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID},

						ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID
								+ " in(" + contactId_Combine + ")", null, null);

		if (postalCursor.moveToFirst()) {

			String contactId = "";
			String state;
			String country;
			String formattedAddress;
			String addressType;
			do {

				contactId = postalCursor
						.getString(postalCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						// String street =
						// postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
						// String poBox =
						// postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
						// String neighborhood =
						// postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.NEIGHBORHOOD));
						// String city =
						// postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
						state = postalCursor
								.getString(postalCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
						// String zipCode =
						// postalCursor.getString(postalCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
						country = postalCursor
								.getString(postalCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
						formattedAddress = postalCursor
								.getString(postalCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
						addressType = AddressType
								.getActualType(Integer.parseInt(postalCursor.getString(postalCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE))));

						// Log.i("ContentProvider",
						// "STREET: "+street+"/nPOBOX: "+poBox+"/nneighborhood: "+neighborhood+"/nCITY: "+city+"/nREGION: "+state+"/nPOSTCODE: "+zipCode
						// +"/nCOUNTRY: "+country+"/nFORMATTED_ADDRESS: "+formattedAddress+"/nADDRESSType: "+addressType);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Address");
						contactItem.setSubtype(addressType);
						contactItem.setValue(formattedAddress);
						// contactItem.setValue2("");
						contactItem.setCountry(country);
						contactItem.setRegion(state);
						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (postalCursor.moveToNext());
		}
		postalCursor.close();

	}

	private void Pad_im(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor imCursor = cr.query(ContactsContract.Data.CONTENT_URI,
				new String[]{ContactsContract.CommonDataKinds.Im.DATA,
						ContactsContract.CommonDataKinds.Im.TYPE,
						ContactsContract.CommonDataKinds.Im.PROTOCOL,
						ContactsContract.CommonDataKinds.Im.CONTACT_ID},
				ContactsContract.CommonDataKinds.Im.CONTACT_ID + " in("
						+ contactId_Combine + ") AND "
						+ ContactsContract.Data.MIMETYPE + "='"
						+ ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE
						+ "'", null, null);

		if (imCursor.moveToFirst()) {
			String contactId = "";
			String im;
			String imProtocolType;

			do {
				contactId = imCursor
						.getString(imCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().equals(contactId)) {
						im = imCursor
								.getString(imCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
						// String imType =
						// IMType.getActualType(Integer.parseInt(imCursor.getString(imCursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))));
						imProtocolType = IMType
								.getActualProtocolType(Integer.parseInt(imCursor.getString(imCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL))));

						// Log.i("ContentProvider", "IM:"+im);
						// Log.i("ContentProvider", "IMTYPE:"+imType);
						// Log.i("ContentProvider",
						// "IMTPROTOCOLYPE:"+imProtocolType);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("IM");
						contactItem.setSubtype(imProtocolType);
						contactItem.setValue(im);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");
						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (imCursor.moveToNext());
		}
		imCursor.close();

	}

	private void Pad_website(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor websiteCursor = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.Website.URL,
								ContactsContract.CommonDataKinds.Website.TYPE,
								ContactsContract.CommonDataKinds.Website.CONTACT_ID},
						ContactsContract.CommonDataKinds.Website.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE
								+ "'", null, null);

		if (websiteCursor.moveToFirst()) {
			String contactId = "";
			String website;
			String websiteType;
			do {

				contactId = websiteCursor
						.getString(websiteCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Website.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getContactId().equals(contactId)) {
						website = websiteCursor
								.getString(websiteCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
						websiteType = WebsiteType
								.getActualType(Integer.parseInt(websiteCursor.getString(websiteCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))));

						// Log.i("ContentProvider", "website: "+ website);
						// Log.i("ContentProvider", "websiteType: "+
						// websiteType);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Website");
						contactItem.setSubtype(websiteType);
						contactItem.setValue(website);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");
						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);
						break;
					}
				}

			} while (websiteCursor.moveToNext());
		}
		websiteCursor.close();

	}

	private void Pad_note(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor noteCursor = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.Note.NOTE,
								ContactsContract.CommonDataKinds.Note.CONTACT_ID},
						ContactsContract.CommonDataKinds.Note.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE
								+ "'", null, null);

		if (noteCursor.moveToFirst()) {

			String contactId = "";
			String note;
			do {

				contactId = noteCursor
						.getString(noteCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Note.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().endsWith(contactId)) {
						note = noteCursor
								.getString(noteCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));

						// Log.i("ContentProvider", "note:"+note);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Note");
						// contactItem.setSubtype("");
						contactItem.setValue(note);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");
						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (noteCursor.moveToNext());

		}
		noteCursor.close();

	}

	private void Pad_nickname(Collection<Contact> contactHeader,
			String contactId_Combine) {

		Cursor nicknameCursor = cr
				.query(ContactsContract.Data.CONTENT_URI,
						new String[]{
								ContactsContract.CommonDataKinds.Nickname.NAME,
								ContactsContract.CommonDataKinds.Nickname.CONTACT_ID},
						ContactsContract.CommonDataKinds.Nickname.CONTACT_ID
								+ " in("
								+ contactId_Combine
								+ ") AND "
								+ ContactsContract.Data.MIMETYPE
								+ "='"
								+ ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE
								+ "'", null, null);

		if (nicknameCursor.moveToFirst()) {

			String contactId = "";
			String nickname;
			do {

				contactId = nicknameCursor
						.getString(nicknameCursor
								.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.CONTACT_ID));

				for (Contact contact : contactHeader) {
					if (contact.getPhoneContactId().endsWith(contactId)) {

						nickname = nicknameCursor
								.getString(nicknameCursor
										.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
						// Log.i("ContentProvider", "nickname: "+nickname);

						ContactItem contactItem = new ContactItem();
						contactItem.setType("Nickname");
						// contactItem.setSubtype("");
						contactItem.setValue(nickname);
						// contactItem.setValue2("");
						// contactItem.setCountry("");
						// contactItem.setRegion("");
						contact.getContactItem().add(contactItem);
						// contactItemHeader.add(contactItem);

						break;
					}
				}

			} while (nicknameCursor.moveToNext());

		}

		nicknameCursor.close();

	}

	private Collection<Contact> getContactHeader(List<String> contactIds) {
		Collection<Contact> contactHeader = new ArrayList<Contact>();
		contactHeader = Generate_Contact_Submit_Form(contactIds);
		String contactId_Combine = getContactId_Combine(contactIds);
		Pad_names(contactHeader, contactId_Combine);
		Pad_Event(contactHeader, contactId_Combine);
		Pad_Phones(contactHeader, contactId_Combine);
		Pad_Organization(contactHeader, contactId_Combine);
		Pad_email(contactHeader, contactId_Combine);
		Pad_postal(contactHeader, contactId_Combine);
		Pad_im(contactHeader, contactId_Combine);
		Pad_website(contactHeader, contactId_Combine);
		Pad_note(contactHeader, contactId_Combine);
		Pad_nickname(contactHeader, contactId_Combine);

		return contactHeader;
	}

	public void WriteFile(String jsonString) {
		try {
			File f = new File(Environment.getExternalStorageDirectory(), "EC");

			if (!f.exists()) {
				f.mkdir();
			}

			File gcmfile = new File(f, "registrationId.txt");
			FileWriter writer = new FileWriter(gcmfile);

			writer.append(jsonString);
			writer.flush();
			writer.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
